﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;

namespace Weiz.TaskManager.Common
{
    public  class FileManageService
    {
        /// <summary>
        /// 创建标识：温龙
        /// 功能描述:文件上传
        /// </summary>
        /// <param name="request"></param>
        public  string  UploadFile(FileUploadMessage request)
        {
            FileStream targetStream = null;
            string filePath = Path.Combine(request.SavePath, request.FileName);
            string fileDisk = AppDomain.CurrentDomain.BaseDirectory; 
            string dirName = fileDisk + request.SavePath;
            string fileUpPath = dirName + request.FileName;
            if (!request.FileData.CanRead)
            {
                throw new Exception("数据流不可读!");
            }
            if (!Directory.Exists(dirName))
            {
                Directory.CreateDirectory(dirName);
            }
            using (targetStream = new FileStream(fileUpPath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                const int bufferLen = 4096;
                byte[] buffer = new byte[bufferLen];
                int count = 0;
                while ((count = request.FileData.Read(buffer, 0, bufferLen)) > 0)
                {
                    targetStream.Write(buffer, 0, count);
                }
                targetStream.Close();
                request.FileData.Close();
            }
            return filePath;
        }

        /// <summary>
        /// 创建标识:温龙
        /// 功能描述:文件删除
        /// </summary>
        /// <param name="fileName"></param>
        public void RemoveFile(string fileName)
        {
            string fileDisk = ConfigurationManager.AppSettings["WebFilePath"];
            string filePath = fileDisk + fileName;
            FileInfo info = new FileInfo(filePath);
            if (info.Exists)
            {
                info.Delete();
            }
        }
    }

 
    public class FileUploadMessage
    {
      
        public string SavePath;

       
        public string FileName;

      
        public Stream FileData;
    }

}
